package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ReflectDemo03 {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException {
        Class<Person> personClass = Person.class;
        Person person = personClass.newInstance();
        Method setName = personClass.getMethod("setName", String.class);
        Method setAge = personClass.getMethod("setAge", int.class);
        setName.invoke(person, "xiaoming");
        setAge.invoke(person, 12);
        System.out.println(person);
        Method[] declaredMethods = personClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            int modifiers = declaredMethod.getModifiers();
            if (modifiers == Modifier.PUBLIC) {
                System.out.println(declaredMethod.getName() + "公开的方法");
            }
        }
    }

}
